/* 
  通过nodejs 中的  
  HTTP模块、URL模块、fs模块、Path模块
  来构建静态资源服务器
*/

const http = require('http')
const fs = require('fs')
const path = require('path')
const url = require('url')
const mimer = require('./extends/mimer.js')
const router = require('./router/router.js')
const config = global.config
const PORT = config.PORT || 8001

// 启动
http.createServer((req, res) => {
  res.setHeader('Access-Control-Allow-Headers', config.allowHeaders)
  if (config.isCrossOrigin) {
    res.setHeader('Access-Control-Allow-Origin', '*') // 允许跨域访问
  }
  if (config.isDynamicOrigin) {
    res.setHeader('Access-Control-Allow-Origin', req.headers.origin) // 动态允许源
  }
  let method = req.method.toLowerCase()
  if (method === 'options' || method === 'head') {
    res.writeHead(200, { 'Content-Type': 'text/html;charset="utf-8"' })
    res.write('OK')
    res.end() // 结束响应
    return true
  }
  var pathname = url.parse(req.url).pathname   // 通过url.parse()直接获取请求的数据路径
  let query = url.parse(req.url).query
  pathname = pathname.replace(/\.\.\//g, '').replace(/\/\//g, '/')
  // 过滤请求
  if (pathname === '/') {
    pathname = '/pages/index.html' // 默认首页
  } else if (pathname === '/favicon.ico') {
    pathname = '/pages/favicon.ico' // 默认图标
  }
  // 服务请求
  let route = router.matchRoute(pathname, method)
  if (route) {
    let defer = router.run(route, req, res)
    Promise.resolve(defer).then(result => {
      if (result && result.status) {
        let isObject = Object.prototype.toString.call(result) === '[object Object]' || /^\s*(\[|\{})/.test(result)
        let contentType = isObject ? 'application/json;charset="utf-8"' : 'text/html;charset="utf-8"'
        res.writeHead(result.status, { 'Content-Type': contentType })
        res.write(isObject ? JSON.stringify(result) : (result || ''))
        res.end() // 结束响应
      }
    }).catch((e) => {
      s500(res)
      console.log(e)
    })
    return true
  }
  // 普通请求
  else {
    // 请求目录验证
    let isAccess = false
    let accessDirs = Array.isArray(config.accessDirs) ? config.accessDirs : []
    let repNode = accessDirs.find(item => String(pathname).toLowerCase().indexOf(String(item.path).toLowerCase()) > -1)
    accessDirs.forEach(item => {
      if (
        String(pathname).toLowerCase().indexOf(String(item).toLowerCase()) > -1 || repNode) {
        isAccess = true
      }
    })
    // 无该目录请求权限
    if (!isAccess) {
      s401(res)
      return true
    }
    let dir = path.dirname(__filename) + pathname
    // 目录转换
    if (repNode) {
      let rpath = pathname
      let replace = repNode.replace
      // 路径转换
      if (Array.isArray(replace)) {
        replace.forEach(item => {
          rpath = rpath.replace(item.match, item.replace)
        })
      } else {
        rpath = pathname.replace(repNode.path, repNode.replace)
      }
      // 参数转换为路径
      let rquery = repNode.query
      if (Array.isArray(rquery)) {
        rquery.forEach(item => {
          if (item.match.test(query)) {
            let value = item.match.exec(query)[0]
            value = value.replace(item.match, item.replace)
            rpath += item.wrap !== false ? '/' + value : value
          }
        })
      }
      pathname = rpath
      dir = path.dirname(__filename) + rpath
    }
    // 读取指定文件
    fs.readFile(path.resolve(config.serverDir, dir), (err, result) => {
      // 文件未找到
      if (err) {
        fs.readFile(config.serverDir + '/pages/404.html', (error, data) => {
          // 404文件读取失败
          if (error) {
            s500(res)
            console.log(error)
            return false
          }
          res.writeHead(404, { 'Content-Type': 'text/html;charset="utf-8"' })
          res.write(data)
          res.end() // 结束响应
        })
      }
      // 正常输出文件
      else {
        // 获取文件的后缀名，根据不同的后缀名返回不同的信息
        var extname = path.extname(pathname)
        // 响应头
        var mime = mimer.getMime(extname) // 获取文件类型
        res.writeHead(200, { 'Content-Type': '' + mime + ';charset="utf-8"' })
        res.write(result)
        res.end() // 结束响应
      }
    })
  }
}).listen(PORT, () => {
  setTimeout(() => {
    console.log('DMapT Server is running on: http://127.0.0.1:' + PORT)
  }, 500)
  if (/dist|dest/.test(path.resolve(path.dirname(__filename)))) {
    setTimeout(() => {
      console.log('DMapT Front is running on: http://127.0.0.1:' + PORT + '/app/index.html')
    }, 500)
  }
})

// 401服务
function s401 (res) {
  res.writeHead(401, { 'Content-Type': 'text/html;charset="utf-8"' })
  res.write('Service 401 Error')
  res.end() // 结束响应
}

// 500服务
function s500 (res) {
  res.writeHead(500, { 'Content-Type': 'text/html;charset="utf-8"' })
  res.write('Service 500 Error')
  res.end() // 结束响应
}